﻿using Model;
using SmsHelper;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Business
{
    public class ConfigurationBiz
    {
        public int SaveItem(Configuration item)
        {
            var dbContext = new Context();

            dbContext.Configurations.Add(item);
            dbContext.SaveChanges();

            return item.Id;
        }

        public void UpdateItem(Configuration item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public Configuration LoadItem(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.Configurations
                    where item.Id == id
                    select item).First();
        }

        public void RemoveItem(List<Configuration> items)
        {
            var dbContext = new Context();

            string ids = string.Empty;

            foreach (var item in items)
            {
                ids += item.Id + ",";
            }

            if (ids.EndsWith(","))
            {
                ids = ids.Substring(0, ids.Length-1);

                string deleteCommand = string.Format(Constants.DeleteCommand, "Configuration", ids);
                dbContext.Database.ExecuteSqlCommand(deleteCommand);
            }            
        }

        public List<Configuration> LoadItems()
        {
            var dbContext = new Context();
            return (from item in dbContext.Configurations
                    orderby item.Key
                    select item).ToList();
        }

        public SmsConfiguration GetSmsConfiguration()
        {
            SmsConfiguration result = null;

            try
            {
                var dbContext = new Context();
                var list = (from item in dbContext.Configurations
                            select item);

                result = new SmsConfiguration();
                foreach (var item in list)
                {
                    if (string.Equals(item.Key, Constants.ConfigurationSMsPort, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        result.PortName = Convert.ToString(item.Value);
                    }

                    if (string.Equals(item.Key, Constants.ConfigurationSMsDataBits, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        result.DataBits = Convert.ToInt32(item.Value);
                    }

                    if (string.Equals(item.Key, Constants.ConfigurationSMsBaudRate, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        result.BaudRate = Convert.ToInt32(item.Value);
                    }

                    if (string.Equals(item.Key, Constants.ConfigurationSMsReadTimeout, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        result.ReadTimeout = Convert.ToInt32(item.Value);
                    }

                    if (string.Equals(item.Key, Constants.ConfigurationSMsWriteTimeout, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        result.WriteTimeout = Convert.ToInt32(item.Value);
                    }
                }
            }
            catch(Exception ex)
            {
                result = null;
            }

            return result;
        }

        public Configuration Detail(string key)
        {
            var dbContext = new Context();
            return (from item in dbContext.Configurations
                    where item.Key == key
                    select item).FirstOrDefault();
        }
    }
}
